﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using CloudPms.Common.CacheProvider;
using CloudPms.Common.Log;
using CloudPms.Domain.Interface;
using CloudPms.Domain.Repository;
using Microsoft.Practices.Unity;

namespace CloudPms.Logic.Business
{
    /// <summary>
    /// 中央缓存控制台
    /// <para>--------------------------</para>
    /// <para>@Author: cnalexi@gmail.com</para> 
    /// <para>@Create: 2012/10/06</para>  
    /// <example>
    /// <code>
    /// 引用方法：
    ///     hello world
    /// </code>
    /// </example> 
    /// </summary>
    public class CacheMonitor
    {
        private static volatile CacheMonitor _instance;

        private static readonly object lockThis = new object();

        private Timer _cacheTimer;

        private const int _delyTime = 5000;

        private CacheMonitor()
        {
        }

        public static CacheMonitor GetInstance()
        {
            if (_instance == null)
            {
                lock (lockThis)
                {
                    if (_instance == null)
                    {
                        _instance = new CacheMonitor();
                        Thread.MemoryBarrier();
                    }

                }
            }
            return _instance;
        }

        /// <summary>
        /// 启动定时器
        /// </summary>
        private void StartTimer()
        {
            TimerCallback timerCall = RedisCacheRefresh;

            Random ran = new Random(DateTime.Now.Millisecond);

            int looptime = ran.Next(30, 60) * 100; //正式*10000 //300000 ，5分钟-10分钟内的随机数

            _cacheTimer = new Timer(timerCall, null, _delyTime, looptime);

        }

        /// <summary>
        /// 初始化缓存控制台
        /// </summary>
        public void Initialization()
        {
            try
            {
                //查看缓存状态 是否正在初始化(过期时间2小时) 
                if (CenterCacheInit.CanStartCreating())
                {
                    //更改缓存初始化状态

                    //初始化营业日
                    HotelBizDayCache.Instance.Initialization();

                    //初始化房态缓存
                    RoomStatusCache.Instance.Initialization();

                    CenterCacheInit.Complete();
                }

                StartTimer();
            }
            catch (Exception ex)
            {
                OperateLog.ErrorLog("初始化中央缓存失败，请检查数据库连接和缓存服务器状态",ex);
            }

        }

        /// <summary>
        /// 刷新REDIS的CACHE
        /// </summary>
        /// <param name="timerInfo"></param>
        private void RedisCacheRefresh(object timerInfo)
        {

            try
            {
                //LoadRoomStatus


                //

                var timerstr = "我是一个定时器，定呀定时器。" + DateTime.Now;

                System.Diagnostics.Debug.WriteLine(timerstr);
            }
            catch(Exception ex)
            {
                OperateLog.ErrorLog("更新缓存失败，如果持续出错，请检查数据库连接和缓存服务器状态",ex);

            }
        }
        
        /// <summary>
        /// 关闭定时器
        /// </summary>
        public void Dispose()
        {
            _cacheTimer.Dispose();
            _cacheTimer = null;
        }

    }
}
